<template>
  <div class="fixed-gift-icon" @click.stop="visible = true">
    <img :src="SVG_ICONS.gift_black" alt="gift" />
  </div>
  <!-- 展开显示内容的面板 -->
  <div class="exclusive-offers-mask" v-if="visible">
    <div class="exclusive-offers-modal">
      <button class="close-btn" @click="visible = false">×</button>
      <h3 class="modal-title">EXCLUSIVE OFFERS</h3>
      <div class="modal-desc">All promotional items listed below are final sale*.</div>
      <div class="offers-list">
        <div v-for="(offer, idx) in offersList" :key="idx" @click="goodDetails(offer)">
          <a :href="`/product/${offer.url}.html`" target="_self" :title="offer.name" class="offer-card">
            <img class="offer-img" :src="offer.images[0]" alt="" />
            <div class="offer-info">
              <div class="offer-title" v-html="offer.name"></div>
              <div class="offer-desc" v-html="offer.desc"></div>
              <div class="offer-price">
                <span class="origin" v-if="offer.max_origin_price">{{ currencyStore.currentCurrency.unit }}{{
                  proxy.$ConvertPrice(offer.max_origin_price) }}</span>
                <span class="now">{{ currencyStore.currentCurrency.unit }}{{
                  proxy.$ConvertPrice(offer.min_price) }}</span>
              </div>
            </div>
          </a>
        </div>
      </div>
      <div class="modal-tip">*Final sale: no returns or exchanges.</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance } from 'vue'
import { maxGoodsType } from "@/api/goods"
import { useCurrencyStore } from '@/store';
import { useRouter } from 'vue-router';
import SVG_ICONS from "@/assets/SVG_ICONS.ts";

const router = useRouter();
const { proxy } = getCurrentInstance()
const currencyStore = useCurrencyStore();
const visible = ref(false)
const offersList = ref([])
// 获取优惠商品列表
const getMaxGoodsType = async () => {
  const res = await maxGoodsType()
  offersList.value = res.data
}
getMaxGoodsType()
// 跳转商品详情页
const goodDetails = (product: any) => {
  // window.location.href = `/product/${product.url}.html`
  // 使用 query 参数传递商品 ID
  // router.push({
  //   path: '/details',
  //   query: { id: product.id }
  // });
  visible.value = false
};
</script>

<style scoped>
.fixed-gift-icon {
  position: fixed;
  right: 32px;
  bottom: 40px;
  width: 48px;
  height: 48px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  cursor: pointer;
  transition: box-shadow 0.2s;
}

.fixed-gift-icon:hover {
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.18);
}

.fixed-gift-icon img {
  width: 30px;
  height: 30px;
  object-fit: contain;
}

.exclusive-offers-mask {
  position: fixed;
  z-index: 2000;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: flex-end;
  justify-content: right;
}

.exclusive-offers-modal {
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  width: 370px;
  max-width: 95vw;
  padding: 28px 18px 18px 18px;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  bottom: 50px;
  right: 40px;

}

.close-btn {
  position: absolute;
  top: 18px;
  right: 18px;
  background: none;
  border: none;
  font-size: 26px;
  color: #888;
  cursor: pointer;
  z-index: 2;
}

.modal-title {
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 1px;
  margin-bottom: 6px;
  color: #232323;
}

.modal-desc {
  text-align: center;
  color: #444;
  font-size: 13px;
  margin-bottom: 18px;
}

.offers-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 12px;
  height: 500px;
  overflow-y: scroll;
  padding-right: 10px;
}

.offer-card {
  display: flex;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 15px #00000026;
  padding: 10px;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}

.offer-img {
  width: 78px;
  height: 104px;
  border-radius: 10px;
  object-fit: cover;
  background: #fff;
  flex-shrink: 0;
}

.offer-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.offer-title {
  font-weight: 400;
  font-size: 14px;
  color: #232323;
}

.offer-desc {
  font-size: 13px;
  color: #777;
  margin-bottom: 2px;
}

.offer-desc a {
  color: #b48b5a;
  text-decoration: underline;
}

.offer-price {
  font-size: 13px;
  margin-top: 2px;
}

.offer-price .origin {
  color: #aaa;
  text-decoration: line-through;
  margin-right: 8px;
}

.offer-price .now {
  color: #ae8877;
  font-weight: 400;
  font-size: 12px;
}

.modal-tip {
  text-align: center;
  color: #232323;
  font-size: 12px;
  margin-top: 8px;
}
</style>